<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代理商id" prop="agentId">
        <el-input
          v-model="queryParams.agentId"
          placeholder="请输入代理商id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入商户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['orderParent:orderParent:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['orderParent:orderParent:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['orderParent:orderParent:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['orderParent:orderParent:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderParentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="订单号" align="center" prop="number" />
      <el-table-column label="被调人数" align="center" prop="cdNumber" />
      <el-table-column label="订单总金额" align="center" prop="amount" />
      <el-table-column label="商户id" align="center" prop="storeId" />
      <el-table-column label="代理商id" align="center" prop="agentId" />
      <el-table-column label="套餐id" align="center" prop="setmealId" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['orderParent:orderParent:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['orderParent:orderParent:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改批量订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单号" prop="number">
          <el-input v-model="form.number" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="被调人数" prop="cdNumber">
          <el-input v-model="form.cdNumber" placeholder="请输入被调人数" />
        </el-form-item>
        <el-form-item label="订单总金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入订单总金额" />
        </el-form-item>
        <el-form-item label="商户id" prop="storeId">
          <el-input v-model="form.storeId" placeholder="请输入商户id" />
        </el-form-item>
        <el-form-item label="代理商id" prop="agentId">
          <el-input v-model="form.agentId" placeholder="请输入代理商id" />
        </el-form-item>
        <el-form-item label="商户名称" prop="storeName">
          <el-input v-model="form.storeName" placeholder="请输入商户名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门id" />
        </el-form-item>
        <el-divider content-position="center">${subTable.functionName}信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddSysOrder">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteSysOrder">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="sysOrderList" :row-class-name="rowSysOrderIndex" @selection-change="handleSysOrderSelectionChange" ref="sysOrder">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="订单号" prop="number" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.number" placeholder="请输入订单号" />
            </template>
          </el-table-column>
          <el-table-column label="候选人昵称" prop="cdName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cdName" placeholder="请输入候选人昵称" />
            </template>
          </el-table-column>
          <el-table-column label="身份证号" prop="idNumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.idNumber" placeholder="请输入身份证号" />
            </template>
          </el-table-column>
          <el-table-column label="手机号" prop="phone" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.phone" placeholder="请输入手机号" />
            </template>
          </el-table-column>
          <el-table-column label="证书编号" prop="certificate" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.certificate" placeholder="请输入证书编号" />
            </template>
          </el-table-column>
          <el-table-column label="套餐单价" prop="amount" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.amount" placeholder="请输入套餐单价" />
            </template>
          </el-table-column>
          <el-table-column label="授权种类" prop="authorizationType" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.authorizationType" placeholder="请选择授权种类">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="支付状态 0未支付 1已支付" prop="payStatus" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.payStatus" placeholder="请选择支付状态 0未支付 1已支付">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="订单状态 0发起被调1待授权2背调中 3已出报告" prop="orderStatus" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.orderStatus" placeholder="请选择订单状态 0发起被调1待授权2背调中 3已出报告">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="订单结果1待出具2无风险3低风险4中风险5高风险" prop="orderOutcome" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.orderOutcome" placeholder="请输入订单结果1待出具2无风险3低风险4中风险5高风险" />
            </template>
          </el-table-column>
          <el-table-column label="商户id" prop="storeId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.storeId" placeholder="请输入商户id" />
            </template>
          </el-table-column>
          <el-table-column label="商户名称" prop="storeName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.storeName" placeholder="请输入商户名称" />
            </template>
          </el-table-column>
          <el-table-column label="代理商id" prop="agentId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.agentId" placeholder="请输入代理商id" />
            </template>
          </el-table-column>
          <el-table-column label="报告路径" prop="reportUrl" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.reportUrl" placeholder="请输入报告路径" />
            </template>
          </el-table-column>
          <el-table-column label="套餐id" prop="setmealId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.setmealId" placeholder="请输入套餐id" />
            </template>
          </el-table-column>
          <el-table-column label="用户id" prop="userId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.userId" placeholder="请输入用户id" />
            </template>
          </el-table-column>
          <el-table-column label="部门id" prop="deptId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.deptId" placeholder="请输入部门id" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrderParent, getOrderParent, delOrderParent, addOrderParent, updateOrderParent } from "@/api/orderParent/orderParent";

export default {
  name: "OrderParent",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedSysOrder: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 批量订单表格数据
      orderParentList: [],
      // ${subTable.functionName}表格数据
      sysOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        number: null,
        orderStatus: null,
        agentId: null,
        setmealId: null,
        storeName: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询批量订单列表 */
    getList() {
      this.loading = true;
      listOrderParent(this.queryParams).then(response => {
        this.orderParentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        number: null,
        cdNumber: null,
        payStatus: null,
        orderStatus: null,
        amount: null,
        storeId: null,
        agentId: null,
        setmealId: null,
        storeName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        userId: null,
        deptId: null
      };
      this.sysOrderList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加批量订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrderParent(id).then(response => {
        this.form = response.data;
        this.sysOrderList = response.data.sysOrderList;
        this.open = true;
        this.title = "修改批量订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.sysOrderList = this.sysOrderList;
          if (this.form.id != null) {
            updateOrderParent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrderParent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除批量订单编号为"' + ids + '"的数据项？').then(function() {
        return delOrderParent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** ${subTable.functionName}序号 */
    rowSysOrderIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** ${subTable.functionName}添加按钮操作 */
    handleAddSysOrder() {
      let obj = {};
      obj.number = "";
      obj.cdName = "";
      obj.idNumber = "";
      obj.phone = "";
      obj.certificate = "";
      obj.amount = "";
      obj.authorizationType = "";
      obj.payStatus = "";
      obj.orderStatus = "";
      obj.orderOutcome = "";
      obj.storeId = "";
      obj.storeName = "";
      obj.agentId = "";
      obj.reportUrl = "";
      obj.setmealId = "";
      obj.remark = "";
      obj.userId = "";
      obj.deptId = "";
      this.sysOrderList.push(obj);
    },
    /** ${subTable.functionName}删除按钮操作 */
    handleDeleteSysOrder() {
      if (this.checkedSysOrder.length == 0) {
        this.$modal.msgError("请先选择要删除的${subTable.functionName}数据");
      } else {
        const sysOrderList = this.sysOrderList;
        const checkedSysOrder = this.checkedSysOrder;
        this.sysOrderList = sysOrderList.filter(function(item) {
          return checkedSysOrder.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleSysOrderSelectionChange(selection) {
      this.checkedSysOrder = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('orderParent/orderParent/export', {
        ...this.queryParams
      }, `orderParent_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
